<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/5/14
 * Time: 11:11
 */

namespace app\commi\controller;

use think\Db;
use think\Request;

class Notice extends Base
{
    /**
     * 消息列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $notice_title = trim(input('notice_title'), ' ');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $where['notice_type'] = 1;
            $where['notice_committee_id'] = session('com_id');
            if(isset($notice_title) && $notice_title != ''){
                $where['notice_title'] = ['like', '%' . $notice_title . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['notice_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['notice_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['notice_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')->where($where)->count();// 获取总条数
            $lists = Db::name('notice')
                ->field('notice_id, notice_title, notice_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('notice_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 发布消息
     * @return mixed|\think\response\Json
     */
    public function noticeAdd(){
        if(Request::instance()->isPost()){
            $notice_title = trim(input('notice_title'), ' ');
            $notice_content = trim(input('notice_content'), ' ');
            $insert = [
                'notice_title' => $notice_title,
                'notice_content' => $notice_content,
                'notice_range' => 1,
                'notice_user' => -1,
                'notice_read_user' => ',0,',
                'notice_del_user' => ',0,',
                'notice_type' => 1,
                'notice_committee_id' => session('com_id'),
                'notice_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_notice')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '发布成功']);
            }else{
                return json(['code' => 100, 'msg' => '发布失败']);
            }
        }
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('notice/noticeadd');
        }
    }
    
    /**
     * 消息详情
     * @return mixed
     */
    public function noticeDetail(){
        $notice_id = input('notice_id');
        $notice = Db::table('up_notice')
            ->where('notice_id', $notice_id)
            ->field('notice_id, notice_title, notice_content, notice_time')
            ->find();
        return $this->fetch('notice/noticedetail', ['notice' => $notice]);
    }
}